package com.fbs.wowo.finance.web;

import com.fbs.wowo.base.BaseController;
import com.fbs.wowo.base.Response;
import com.fbs.wowo.exception.CreateEntityFailedException;
import com.fbs.wowo.exception.InputVerifyFailedException;
import com.fbs.wowo.finance.service.PayService;
import com.fbs.wowo.finance.util.BillGenerator;
import com.fbs.wowo.finance.vo.WXAppPayResult;
import com.fbs.wowo.util.netutil.NetUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

@RestController
@RequestMapping("/pay")
public class PayController extends BaseController {

    /**
     * 我的购买
     */
    @GetMapping("/myBuy")
    public Response myBuy(@RequestParam String userId) {
        return success(payService.buyList(userId));
    }
//    public Response myBuy(@RequestParam String userId, @RequestParam Byte productCategory) {
//        return success(payService.buyList(userId, productCategory));
//    }

    /**
     *
     * 用户购买记录
     * @param userId   买家id
     * @param pageNo    页码
     * @param pageSize  每页数据量
     * @return payment
     */
    @GetMapping("/list")
    public Response list(
            @RequestParam String userId,
            @RequestParam(required = false, defaultValue = "1") Integer pageNo,
            @RequestParam(required = false, defaultValue = "1") Integer pageSize) {
        return success(payService.orderList(userId, pageNo, pageSize));
    }
    /**
     * 查询是否付过费
     * @param buyerId          用户id
     * @param productId        产品id
     * @param productCategory  打赏或其他
     * @return 1 没付过 2 付过
     */
    @GetMapping("/paid")
    public Response paid(@RequestParam String buyerId, @RequestParam String productId, @RequestParam Byte productCategory) {
        return success(payService.paid(buyerId, productId, productCategory));
    }
    /**
     * 查询余额
     * @param userId 用户id
     * @return       余额
     */
    @GetMapping("/balance")
    public Response balance(@RequestParam String userId) {
        return success(payService.balance(userId));
    }

    /**
     * 统一支付
     */
    @PostMapping("/pay")
    public Response pay (
            @RequestParam Byte tradeType,
            @RequestParam String buyerId,
            @RequestParam BigDecimal fee,
            @RequestParam String productId,
            @RequestParam Byte productType,
            @RequestParam Byte productCategory,
            HttpServletRequest request,
            HttpServletResponse response) {
        switch (tradeType) {
            case BillGenerator.PayOri:
                return success(payService.submitorderBalance(buyerId, fee, productId, productType, productCategory));
            case BillGenerator.PayWXApp:
                String ipAddress = NetUtils.getIpAddress(request);

                WXAppPayResult unifiedorder = payService.submitorderWXApp(
                        ipAddress,
                        buyerId,
                        fee,
                        productId,
                        productType,
                        productCategory);
                return success(unifiedorder);
            default:return null;
        }

    }
    /**
     * 余额支付 + 密码校验
     * @param password         支付密码
     * @param buyerId          用户id
     * @param fee              金额
     * @param productId        产品id
     * @param productType      产品类型
     * @param productCategory  打赏或其他
     * @return                 支付状态
     * @throws CreateEntityFailedException  异常
     * @throws InputVerifyFailedException   密码错误
     */
    @PostMapping("/balancepaypass")
    public Response buyByBalanceVerifyPass(
            @RequestParam String password,
            @RequestParam String buyerId,
            @RequestParam BigDecimal fee,
            @RequestParam String productId,
            @RequestParam Byte productType,
            @RequestParam Byte productCategory) {
        return success(payService.submitorderBalanceVerifyPass(password, buyerId, fee, productId, productType, productCategory));
    }
    /**
     * 余额支付
     * @param buyerId          用户id
     * @param fee              金额
     * @param productId        产品id
     * @param productType      产品类型
     * @param productCategory  打赏或其他
     * @return                 支付状态
     */
    @PostMapping("/balancepay")
    public Response buyByBalance(
            @RequestParam String buyerId,
            @RequestParam BigDecimal fee,
            @RequestParam String productId,
            @RequestParam Byte productType,
            @RequestParam Byte productCategory) {
        return success(payService.submitorderBalance(buyerId, fee, productId, productType, productCategory));

    }

    // 1 文章 图文
    // 2 音频
    // 3 专辑
    // 4 直播
    // 1 打赏
    // 2 购买
    /**
     * 微信扫码支付 NATIVE
     * @param buyerId           买家id
     * @param fee               费用
     * @param productId         产品id
     * @param productType       产品类型
     * @param productCategory   产品分类
     */
    @PostMapping("/pcpay")
    public void submitorderWXNative(
            @RequestParam String buyerId,
            @RequestParam BigDecimal fee,
            @RequestParam String productId,
            @RequestParam Byte productType,
            @RequestParam Byte productCategory,
            HttpServletRequest request,
            HttpServletResponse response) {
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            payService.submitorderWXNative(
                    NetUtils.getIpAddress(request),
                    buyerId,
                    fee,
                    productId,
                    productType,
                    productCategory,
                    outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 微信App支付 APP
     * @param buyerId           买家id
     * @param fee               费用
     * @param productId         产品id
     * @param productType       产品类型
     * @param productCategory   产品分类
     * @return                  返回给app端结果
     */
    @PostMapping("/apppay")
    public Response submitorderWXApp(
            @RequestParam String buyerId,
            @RequestParam BigDecimal fee,
            @RequestParam String productId,
            @RequestParam Byte productType,
            @RequestParam Byte productCategory,
            HttpServletRequest request) {
        String ipAddress = NetUtils.getIpAddress(request);

        WXAppPayResult unifiedorder = payService.submitorderWXApp(
                ipAddress,
                buyerId,
                fee,
                productId,
                productType,
                productCategory);
        return success(unifiedorder);
    }

    /**
     * 微信异步通知
     * @param data  微信传递xml数据
     * @return      通知微信成功信息
     */
    @RequestMapping("/wxnotice")
    public String wxnotive(@RequestBody String data) {
        return payService.wxNotice(data);
    }

    /**
     * 主动查询订单号
     * @param orderNo 订单号
     * @return        交易结果
     *                  NOTPAY,     // 未支付
     *                  CLOSED,     // 关闭交易
     *                  PAYERROR,   // 支付错误
     *                  SUCCESS,    // 支付成功
     *                  REFUND;     // 退款
     */
    @PostMapping("/syn")
    public Response syn(@RequestParam String orderNo) {
        return success(payService.syn(orderNo));
    }

    @Autowired
    private PayService payService;
}
